#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <SDL.h>
#include <SDL/SDL_ttf.h>
#include "dim.h"
#include "interface.h"
#include "creation.h"
#include "saisie.h"
#include "list_fichier.h"

int quitter = 0, yoshi_dance = 0, yoshi_delay = 0, decalage_selection = 0, selection_niveau = 0, jeu = 0;
SDL_Surface * screen,  * background_menu, * yoshi, * print_message;
SDL_Rect * pos_bg, * pos_yoshi, * move_yoshi, * pos_message;
TTF_Font * police_menu ;
list_fichier * l_f ;
char chemin_niveau[100] = "./Niveaux" ;
char plus[20] = "" ;
SDL_Color color_message = {255, 153, 0};

void Yoshi_Selection()
{
    int i ;
    move_yoshi->w = 80;
    move_yoshi->h = 130;
    move_yoshi->x = 0 ;
    move_yoshi->y = 1660;
    for (i = 0; i < 4; i++) {
        while (move_yoshi->x < 450) {
            SDL_BlitSurface(background_menu, NULL, screen, pos_bg) ;
            SDL_BlitSurface(yoshi, move_yoshi, screen, pos_yoshi) ;
            SDL_BlitSurface(print_message, NULL, screen, pos_message) ;
            SDL_UpdateRect(screen, 0, 0, 0, 0) ;
            SDL_Delay(100) ;
            move_yoshi->x += 75 ;
        }
    }
    move_yoshi->w = 90;
    move_yoshi->h = 130;
    move_yoshi->x = 460 ;
    SDL_BlitSurface(background_menu, NULL, screen, pos_bg) ;
    SDL_BlitSurface(yoshi, move_yoshi, screen, pos_yoshi) ;
    SDL_BlitSurface(print_message, NULL, screen, pos_message) ;
    SDL_UpdateRect(screen, 0, 0, 0, 0) ;
    move_yoshi->w = 100;
    move_yoshi->h = 130;
    move_yoshi->x = 550 ;
    SDL_BlitSurface(background_menu, NULL, screen, pos_bg) ;
    SDL_BlitSurface(yoshi, move_yoshi, screen, pos_yoshi) ;
    SDL_UpdateRect(screen, 0, 0, 0, 0) ;
    SDL_Delay(200) ;

}

void Yoshi_Dance()
{
        if (yoshi_delay == 0) {
            if ( (yoshi_dance == 0 && move_yoshi->x < 4 * move_yoshi->w) || move_yoshi->x <= 0 ) {
                yoshi_dance = 0;
                move_yoshi->x += move_yoshi->w;
            } else {
                yoshi_dance = 1;
                move_yoshi->x -= move_yoshi->w;
            }
            SDL_BlitSurface(background_menu, NULL, screen, pos_bg) ;
            SDL_BlitSurface(yoshi, move_yoshi, screen, pos_yoshi) ;
            afficher_niveau(screen, police_menu, l_f, decalage_selection, 1) ;
            SDL_UpdateRect(screen, 0, 0, 0, 0) ;
            yoshi_delay = 100000;
        } else {
            yoshi_delay -= 1 ;
        }
}

void HandleEventInMenu(SDL_Event event)
{
    switch (event.type) {
    case SDL_KEYDOWN:
        switch(event.key.keysym.sym) {

        case SDLK_UP:
            if (selection_niveau == 0) {
                    if (decalage_selection > 0) {
                        decalage_selection -= 1;
                        SDL_Delay(200) ;
                    }
            } else {
                selection_niveau -= 1;
                pos_yoshi->y -= 100;
                SDL_Delay(200) ;
            }
            break;

        case SDLK_DOWN:
            if (selection_niveau >= 3) {
                if (decalage_selection < l_f->nb_fichier - 4) {
                    decalage_selection += 1;
                    SDL_Delay(200) ;
                }
            } else {
                if (selection_niveau < l_f->nb_fichier - 1) {
                    selection_niveau += 1;
                    pos_yoshi->y += 100;
                    SDL_Delay(200) ;
                }
            }
            break;

        case SDLK_ESCAPE:
            previous_file(chemin_niveau) ;
            supp_list_fichier(l_f) ;
            if (chemin_niveau[0] != '.') {
                quitter = 1;
            } else {
                l_f = lister_fichier(chemin_niveau) ;
                pos_yoshi->x = 400;
                pos_yoshi->y = 300;
                move_yoshi->x = 0 ;
                move_yoshi->y -= 115 ;
                move_yoshi->w = 95;
                selection_niveau = 0 ;
                decalage_selection = 0;
                SDL_Delay(500) ;
            }
            break;

        case SDLK_RETURN:
        {
            int i ;
            fichier * fichier_choisi = l_f->premier ;
            for (i = 0 ; i<selection_niveau + decalage_selection ; i++) {
                fichier_choisi = fichier_choisi->suivant ;
            }
            if (test_txt(fichier_choisi->nom)) {
                Yoshi_Selection();
                strcat(chemin_niveau, "/") ;
                strcat(chemin_niveau, fichier_choisi->nom) ;
                if ( strcmp(fichier_choisi->nom , "Nouveau Niveau.txt") == 0 ) {
                    CreerPentomino(screen, chemin_niveau) ;
                } else {
                    JouerPentomino(screen, chemin_niveau) ;
                }
                supp_list_fichier(l_f) ;
                strcpy(chemin_niveau ,"./Niveaux") ;
                strcpy(plus, "" ) ;
                l_f = lister_fichier(chemin_niveau) ;
                pos_yoshi->x = 400;
                pos_yoshi->y = 300;
                selection_niveau = 0 ;
                decalage_selection = 0;
                jeu = 0 ;
                SDL_Delay(500) ;
                SDL_BlitSurface(background_menu, NULL, screen, pos_bg) ;
                move_yoshi->x = 0;
                move_yoshi->y = 0;
                move_yoshi->w = 95;
                move_yoshi->h = 115;
            } else {
                strcpy(plus, fichier_choisi->nom) ;
                strcat(chemin_niveau, "/") ;
                strcat(chemin_niveau, plus) ;
                pos_yoshi->x = 400;
                pos_yoshi->y = 300;
                move_yoshi->x = 0 ;
                move_yoshi->y += 115;
                move_yoshi->w = 103;
                selection_niveau = 0 ;
                decalage_selection = 0;
                jeu = 1;
                supp_list_fichier(l_f) ;
                l_f = lister_fichier(chemin_niveau) ;
                SDL_Delay(500) ;
            }
            break;
        }

        default:
            break;

        break;
        }
    }
}


int main(int argc, char** argv)
{
    SDL_Event event ;
    SDL_Init(SDL_INIT_VIDEO) ;
    SDL_WM_SetCaption("Pentyoshino", "Pentyoshino") ;
    screen = SDL_SetVideoMode(L_ECRAN, H_ECRAN, 32, SDL_HWSURFACE) ;
    SDL_Surface *temp = SDL_LoadBMP("Yoshi_Menu.bmp");
    background_menu = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp) ;
    pos_bg = (SDL_Rect *) malloc(sizeof(SDL_Rect)) ;
    pos_bg->x = 0;
    pos_bg->y = 0;
    temp = SDL_LoadBMP("Yoshi_Sprite.bmp");
    yoshi = SDL_DisplayFormat(temp);
    SDL_FreeSurface(temp) ;
    SDL_SetColorKey(yoshi, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(screen->format, 0, 64, 128)) ;
    pos_yoshi = (SDL_Rect *) malloc(sizeof(SDL_Rect)) ;
    pos_yoshi->x = 400;
    pos_yoshi->y = 300;
    move_yoshi = (SDL_Rect *) malloc(sizeof(SDL_Rect)) ;
    move_yoshi->x = 0;
    move_yoshi->y = 0;
    move_yoshi->w = 95;
    move_yoshi->h = 115;
    TTF_Init() ;
    police_menu = TTF_OpenFont("yoshisst.ttf", 50) ;
    l_f = lister_fichier(chemin_niveau) ;
    while (quitter != 1) {
        Yoshi_Dance();
        SDL_PollEvent(&event) ;
        HandleEventInMenu(event) ;
    }
  TTF_CloseFont(police_menu);
  TTF_Quit();
  SDL_FreeSurface(yoshi) ;
  SDL_FreeSurface(background_menu) ;
  SDL_FreeSurface(print_message) ;
  SDL_FreeSurface(screen) ;
  SDL_Quit() ;
  return EXIT_SUCCESS;
}
